using BackData;
using Domain.DTOs.RBAC;
using Domain.RBAC;
using Infratructure;
using MediatR;
using Team.API.Applocation.Command.Users.Departments;

namespace Team.API.Applocation.Handler.Users.Departments
{
    /// <summary>
    /// 更新部门处理器
    /// </summary>
    public class UpdateDepartmentHandler : IRequestHandler<UpdateDepartmentCommand, APIResult<DepartmentDTO>>
    {
        private readonly IRepository<Departmentmodel> departmentRepository;

        public UpdateDepartmentHandler(IRepository<Departmentmodel> departmentRepository)
        {
            this.departmentRepository = departmentRepository;
        }

        public async Task<APIResult<DepartmentDTO>> Handle(UpdateDepartmentCommand request, CancellationToken cancellationToken)
        {
            // 查询部门是否存在
            var department = await departmentRepository.GetByIdAsync(request.Id);
            if (department == null || department.IsDel)
            {
                return APIResult<DepartmentDTO>.Faile("部门不存在");
            }

            // 更新部门信息（只更新非空字段）
            if (!string.IsNullOrWhiteSpace(request.DepartmentName))
            {
                department.DepartmentName = request.DepartmentName.Trim();
            }

            if (request.Description != null)
            {
                department.Description = request.Description;
            }

            if (request.LeaderId.HasValue)
            {
                department.LeaderId = request.LeaderId;
            }

            if (!string.IsNullOrWhiteSpace(request.LeaderName))
            {
                department.LeaderName = request.LeaderName;
            }

            if (request.Sort.HasValue)
            {
                department.Sort = request.Sort.Value;
            }

            if (request.Status.HasValue)
            {
                department.Status = request.Status.Value;
            }

            if (request.Phone != null)
            {
                department.Phone = request.Phone;
            }

            if (request.Email != null)
            {
                department.Email = request.Email;
            }

            if (request.Remark != null)
            {
                department.Remark = request.Remark;
            }

            // 更新审计信息
            //department.Updatedate = DateTime.Now;
            //department.Updatename = "系统管理员"; // TODO: 从当前登录用户获取

            int rows = await departmentRepository.UpdateAsync(department);
            if (rows <= 0)
            {
                return APIResult<DepartmentDTO>.Faile("更新部门失败");
            }

            // 构造返回DTO
            var dto = new DepartmentDTO
            {
                Id = department.Id,
                DepartmentCode = department.DepartmentCode,
                DepartmentName = department.DepartmentName,
                ParentId = department.ParentId,
                Description = department.Description,
                LeaderId = department.LeaderId,
                LeaderName = department.LeaderName,
                Sort = department.Sort,
                Status = department.Status,
                Phone = department.Phone,
                Email = department.Email,
                Remark = department.Remark,
                //UpdateDate = department.Updatedate,
                //UpdateName = department.Updatename
            };

            return APIResult<DepartmentDTO>.Ok("更新部门成功", dto);
        }
    }
}
